

package net.jjjshop.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import net.jjjshop.generator.config.GeneratorStrategy;
import net.jjjshop.generator.constant.GeneratorConstant;
import net.jjjshop.generator.properties.GeneratorProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * spring-boot-jjj代码生成器入口类
 **/
@Component
public class SpringBootJjjGenerator {

    public static final String userName = "root";
    public static final String password = "root";
    public static final String driverName = "com.mysql.jdbc.Driver";
    public static final String url = "jdbc:mysql://192.168.1.103:3306/songshan?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    public static final String mavenModuleName = "songshan-system";
    public static final String parentPackage = "com.songshan.system";
    public static final String author = "wzj";

    /**
     * 获取配置信息
     * @return
     */
    private static GeneratorProperties getGeneratorProperties() {
        GeneratorProperties generatorProperties = new GeneratorProperties();
        // 设置基本信息
        generatorProperties
                .setMavenModuleName(mavenModuleName)
                .setParentPackage(parentPackage)
                //.setModuleName("version")
                .setAuthor(author)
                .setFileOverride(true);

        // 数据源配置
        generatorProperties.getDataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setUsername(userName)
                .setPassword(password)
                .setDriverName(driverName)
                .setUrl(url);

        // 设置表信息
//        generatorProperties.addTable("ssw_user","user_id");
        generatorProperties.addTable("ssw_user_points_log","log_id");
//        generatorProperties.addTable("ssw_user_address","address_id");
//        generatorProperties.addTable("ssw_user_cart","cart_id");
        // 设置表前缀
        generatorProperties.setTablePrefix(Arrays.asList("ssw_"));
        return generatorProperties;
    }

    /**
     * 生成代码
     * @param args
     */
    public static void main(String[] args) {
        String moduleName = "user";
//        GeneratorProperties generatorProperties1 = getGeneratorProperties();
////         仅生成数据库表对应的entity
//        generateEntityCodes(moduleName, generatorProperties1);

//        GeneratorProperties generatorProperties2 = getGeneratorProperties();
//        // 包括基本的通用的entity、service、mapper、xml
//        generateCommonCodes(moduleName, generatorProperties2);

        // 通常情况下为false
        Boolean hasBigDecimalProperty = false;
        GeneratorProperties generatorProperties3 = getGeneratorProperties();
        // 包括基本的controller, 继承父Controller
//        generateModuleCodes(moduleName, hasBigDecimalProperty, generatorProperties3, GeneratorConstant.SUPER_CONTROLLER_CLASS);
        // 包括基本的controller, 无父Controller
//        generateModuleCodes(moduleName, hasBigDecimalProperty, generatorProperties3);
        // 生成所有, 包括entity、service、mapper、xml、vo、param、controller
        generateAllCodes(moduleName, hasBigDecimalProperty, generatorProperties3, GeneratorConstant.SUPER_CONTROLLER_CLASS);
    }

    private static void generateEntityCodes(String moduleName, GeneratorProperties generatorProperties) {
        // 生成配置
        generatorProperties.getGeneratorConfig()
                .setGeneratorStrategy(GeneratorStrategy.SIMPLE)
                .setGeneratorEntity(true)
                .setGeneratorController(false)
                .setGeneratorService(false)
                .setGeneratorServiceImpl(false)
                .setGeneratorMapper(false)
                .setGeneratorMapperXml(false)
                .setGeneratorPageParam(false)
                .setGeneratorQueryVo(false)
                .setRequiresPermissions(false)
                .setPageListOrder(false)
                .setParamValidation(false)
                .setSwaggerTags(true)
                .setOperationLog(true);

        // 全局配置
        generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
                .setOpen(true)
                .setSwagger2(true)
                .setIdType(IdType.AUTO)
                .setDateType(DateType.ONLY_DATE);

        // 包配置
        generatorProperties.getMybatisPlusGeneratorConfig().getPackageConfig()
                .setVo("model." + moduleName + ".resp")
                .setParam("model." + moduleName + ".req")
                .setParent(generatorProperties.getParentPackage())
                .setEntity("domain." + moduleName + ".entity")
                .setMapper("domain." + moduleName + ".mapper")
                .setXml(moduleName)
                .setService("service." + moduleName)
                .setServiceImpl("service." + moduleName + ".impl");

        // 策略配置
        generatorProperties.getMybatisPlusGeneratorConfig().getStrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(false)
                .setControllerMappingHyphenStyle(true)
                .setVersionFieldName(GeneratorConstant.VERSION)
                .setLogicDeleteFieldName(GeneratorConstant.DELETED);

        // 生成代码
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generator(generatorProperties);
    }

    private static void generateCommonCodes(String moduleName, GeneratorProperties generatorProperties) {
        // 生成配置
        generatorProperties.getGeneratorConfig()
                .setGeneratorStrategy(GeneratorStrategy.SIMPLE)
                .setGeneratorEntity(true)
                .setGeneratorController(false)
                .setGeneratorService(true)
                .setGeneratorServiceImpl(true)
                .setGeneratorMapper(true)
                .setGeneratorMapperXml(true)
                .setGeneratorPageParam(false)
                .setGeneratorQueryVo(false)
                .setRequiresPermissions(false)
                .setPageListOrder(true)
                .setParamValidation(false)
                .setSwaggerTags(true)
                .setOperationLog(true);

        // 全局配置
        generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
                .setOpen(true)
                .setSwagger2(true)
                .setIdType(IdType.AUTO)
                .setDateType(DateType.ONLY_DATE);

        // 包配置
        generatorProperties.getMybatisPlusGeneratorConfig().getPackageConfig()
                .setVo("model." + moduleName + ".resp")
                .setParam("model." + moduleName + ".req")
                .setParent(generatorProperties.getParentPackage())
                .setEntity("domain." + moduleName + ".entity")
                .setMapper("domain." + moduleName + ".mapper")
                .setXml(moduleName)
                .setService("service." + moduleName)
                .setServiceImpl("service." + moduleName + ".impl");

        // 策略配置
        generatorProperties.getMybatisPlusGeneratorConfig().getStrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(false)
                .setControllerMappingHyphenStyle(true)
                .setVersionFieldName(GeneratorConstant.VERSION)
                .setLogicDeleteFieldName(GeneratorConstant.DELETED);

        // 生成代码
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generator(generatorProperties);
    }

    private static void generateModuleCodes(String moduleName, Boolean hasBigDecimalProperty, GeneratorProperties generatorProperties) {
        generateModuleCodes(moduleName, hasBigDecimalProperty, generatorProperties, null);
    }

    private static void generateModuleCodes(String moduleName, Boolean hasBigDecimalProperty, GeneratorProperties generatorProperties, String superControllerClass) {
        // 生成配置
        generatorProperties.getGeneratorConfig()
                .setGeneratorStrategy(GeneratorStrategy.ALL)
                .setGeneratorEntity(false)
                .setGeneratorController(true)
                .setGeneratorService(false)
                .setGeneratorServiceImpl(false)
                .setGeneratorMapper(false)
                .setGeneratorMapperXml(false)
                .setGeneratorPageParam(true)
                .setGeneratorQueryVo(true)
                .setRequiresPermissions(false)
                .setPageListOrder(false)
                .setParamValidation(false)
                .setBigDecimalProperty(hasBigDecimalProperty)
                .setSwaggerTags(true)
                .setOperationLog(false);

        // 全局配置
        generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
                .setOpen(true)
                .setSwagger2(true)
                .setIdType(IdType.AUTO)
                .setDateType(DateType.ONLY_DATE);

        // 包配置
        generatorProperties.getMybatisPlusGeneratorConfig().getPackageConfig()
                .setVo("model." + moduleName + ".resp")
                .setParam("model." + moduleName + ".req")
                .setEntity("domain." + moduleName + ".entity")
                .setService("service." + moduleName)
                .setParent(generatorProperties.getParentPackage())
                .setController("controller." + moduleName);

        // 策略配置
        generatorProperties.getMybatisPlusGeneratorConfig().getStrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setSuperControllerClass(superControllerClass)
                .setRestControllerStyle(true)
                .setControllerMappingHyphenStyle(true)
                .setVersionFieldName(GeneratorConstant.VERSION)
                .setLogicDeleteFieldName(GeneratorConstant.DELETED);

        // 生成代码
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generator(generatorProperties);
    }


    private static void generateAllCodes(String moduleName, Boolean hasBigDecimalProperty, GeneratorProperties generatorProperties, String superControllerClass) {
        // 生成配置
        generatorProperties.getGeneratorConfig()
                .setGeneratorStrategy(GeneratorStrategy.ALL)
                .setGeneratorEntity(true)
                .setGeneratorController(true)
                .setGeneratorService(true)
                .setGeneratorServiceImpl(true)
                .setGeneratorMapper(true)
                .setGeneratorMapperXml(true)
                .setGeneratorPageParam(true)
                .setGeneratorQueryVo(true)
                .setRequiresPermissions(false)
                .setPageListOrder(false)
                .setParamValidation(false)
                .setBigDecimalProperty(hasBigDecimalProperty)
                .setSwaggerTags(true)
                .setOperationLog(false);

        // 全局配置
        generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
                .setOpen(true)
                .setSwagger2(true)
                .setIdType(IdType.AUTO)
                .setDateType(DateType.ONLY_DATE);

        // 包配置
        generatorProperties.getMybatisPlusGeneratorConfig().getPackageConfig()
                .setVo("model." + moduleName + ".resp")
                .setParam("model." + moduleName + ".req")
                .setEntity("domain." + moduleName + ".entity")
                .setMapper("domain." + moduleName + ".mapper")
                .setXml(moduleName)
                .setService("service." + moduleName)
                .setParent(generatorProperties.getParentPackage())
                .setController("controller." + moduleName);

        // 策略配置
        generatorProperties.getMybatisPlusGeneratorConfig().getStrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setSuperControllerClass(superControllerClass)
                .setRestControllerStyle(true)
                .setControllerMappingHyphenStyle(true)
                .setVersionFieldName(GeneratorConstant.VERSION)
                .setLogicDeleteFieldName(GeneratorConstant.DELETED);

        // 生成代码
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generator(generatorProperties);
    }

}
